package it.frusso.android.notepad;

import it.frusso.android.BaseActivity;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

/*
 * Main Activity - Contain a list of notes (files)
 */
public class MainActivity extends BaseActivity {
    
	private ListView mainListView = null;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // create DefaultDir
        File defaultDir = new File(Environment.getExternalStorageDirectory() +  Const.DEFAULT_PATH);
        boolean createdFolder = defaultDir.mkdirs();
        Log.d(Const.LOG_TAG, "defaultDir  :(" + createdFolder + ") - " + defaultDir.getAbsolutePath());
        
        
        // Identifica la listView
        mainListView = (ListView)findViewById(R.id.mainListView);
        mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView parent, View v, int position, long id) {
                	onListViewItemClick(position, id);
                }
                
		});
        mainListView.setLongClickable(true);
		
    }
    

	@Override
	protected void onResume() {
		super.onResume();
		String folderPath = getApplicationPreferences().getString("folderPath", null);
		if (folderPath == null) {
			Log.e(Const.LOG_TAG, "Error - Invalid folder Path");
			super.showToastMessage(R.string.preferenceInvalidFolderPath);
			return;	
		}
		File fPath = new File(folderPath);
		String files[] = fPath.list(new FilenameFilter() {
			public boolean accept(File dir, String filename) {
				filename = filename.toUpperCase();
				if (filename.endsWith(".TXT") || // Text Files 
						filename.endsWith(".LOG") ||  // Log files
						filename.endsWith("PWD")) return true; // PasswordProtected files
				return false;
			}
		});
		
		Log.d(Const.LOG_TAG, "Found " + files.length + " files in folder");
		
		// Crea la mappa dei dati da visualizzare
		List<Map<String, String>> dataList = new ArrayList<Map<String, String>>();
		for (String file : files) {
			File fx = new File(fPath, file);
			Map<String, String> map = new HashMap<String, String>();
			map.put("fileName", file);
			map.put("fileSize", "" + fx.length());
			map.put("lastModified", getDateAsString(fx.lastModified()));
			dataList.add(map);
		}
		
		Log.d(Const.LOG_TAG, "Built dataListMap");
		int layoutID = R.layout.xxx_listitem1_view;
		SimpleAdapter mAdapter = new SimpleAdapter(this,  dataList, layoutID,
				new String[] { "fileName", "fileSize", "lastModified"}, 
			    new int[] { R.id.xxx_fileName, R.id.xxx_fileSize, R.id.xxx_lastModified }	     
		);
		mainListView.setAdapter(mAdapter);
	}
	
	
	private void onListViewItemClick(int position, long id) {
		
	}
	
	
	private final int MENUITEM_settingsMenu = 10;
    @Override
    public boolean onPrepareOptionsMenu(android.view.Menu menu) {
    	super.onPrepareOptionsMenu(menu);
    	int order = 1;
    	
    	menu.addSubMenu(100, MENUITEM_settingsMenu, order++, "Preferences").setIcon(android.R.drawable.ic_menu_preferences);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       	super.onOptionsItemSelected(item);
       	switch (item.getItemId()) {
	       	
       		case MENUITEM_settingsMenu : {
	       		moveToActivity(PreferenceActivity.class, null);
	       		break;
	       	}
       	}
       	return true;
    }
}